<?php

require_once('../../config.php');
include('../adodb/adodb-exceptions.inc.php');
include('../adodb/tohtml.inc.php');
include('../adodb/adodb.inc.php');

header('Content-type: text/html; charset=utf-8');

if ( ($_POST['sql']) && ($_POST['c']) )
{
	$sel_config = $c[$_POST['c']];
	$sql = urldecode($_POST['sql']);
	$sql = trim($sql);

	if (strlen($sql) == 0)
	{
		echo 'Query is empty.';
		exit;
	}
	
	//enable profiling
	$is_profiling = false;
	if ( (isset($_POST['profiling'])) && ($_POST['profiling'] == 1) )
	{
		$is_profiling = true;
	}
		
	try
	{
		$dsn = $sel_config['type'] .'://' .$sel_config['username'] .':' .$sel_config['password'] .'@' .$sel_config['host'] .'/' .$sel_config['database'];
		$db = ADONewConnection($dsn);
					
		//execute profiling query
		if ($is_profiling)
		{
			$sql_profiling = 'set profiling=1;';
			$db->Execute($sql_profiling);
		}
		
		$r = $db->Execute($sql);

		if ($r)
		{
			if ($is_profiling)
			{				
				$sql_profiling = "SHOW profiles;";
				$r_profiling = $db->Execute($sql_profiling);
				echo rs2html($r_profiling,'class="profiling"');
			}
			
			echo rs2html($r);
		}
	}
	catch (exception $e)
	{		
		echo $e->msg;
	}
	
}
exit;
?>